
<?php 
include('../../Connections/KRAPPERCON.php');

require_once '../../Classes/PHPExcel/IOFactory.php';
require_once '../../Classes/PHPExcel.php';

$oper = $_GET['oper'];

if ($oper == 'export'){

$f1 = $_GET['t_rv_fini'];
$f2 = $_GET['t_rv_ffin'];

$sql = "call sp_getRegVentas('".$f1."','".$f2."');";

$year = substr($f1,0,4);
$month = substr($f1,5,2);
$nom_mes = nameMes($month);
$periodo = $month."/".$year;

$res = $mysqli->query($sql);
$fila = 0;
$start = 11;
$totneto = 0;
$tottot = 0;
$totiva = 0;

if ($res){
	$excel2 = PHPExcel_IOFactory::createReader('Excel2007');
	// Cargar Plantilla de Registro de Venta
	$excel2 = $excel2->load('../../Plantillas/RegVentas_SUNAT_KRAPPER.xlsx'); // Empty Sheet
	$excel2->setActiveSheetIndex(0);
	$sheet = $excel2->getActiveSheet();
	//->setCellValue('C6', '4')->setCellValue('C7', '7')->setCellValue('C8', '1')->setCellValue('C9', '7');
		
		$sheet->insertNewRowBefore(13,$res->num_rows - 1); 
		
		$pref = "";
		
		while($row = $res->fetch_assoc()){
									
			//continue;
			$anulado = $row['FactAnul'];
			$facfolio = $row['FactFolio'];			
			$tip_doc = $row['ClaDocID'];
			
			if ($anulado==2 || trim($facfolio)==""){
			continue;
			}
			
			
			$fila = $fila + 1;
			$start = $start + 1;
			$tipcli = '';
			
			//echo $tip_doc;			
			if ($tip_doc == 'FC_BL'){
			$tip_doc = '03';
			}
			elseif ($tip_doc == 'FC_FC'){
			$tip_doc = '01';
			}
			elseif (substr($tip_doc,0,4)=='FC_N'){
			$tip_doc = '07';
			}
			
			 
			//echo $tip_doc;
			
			$tipcli = '';
			if ($row['CliTypeID']==1){
			$tipcli = '01';
			}
			elseif ($row['CliTypeID']==2){
			$tipcli = '06';
			}
			elseif ($row['CliTypeID']==3){
			$tipcli = '04';
			}
			elseif ($row['CliTypeID']==0){
			$tipcli = '00';
			}
			
			$f1 = '';
			$f2 = '';
			$f3 = '';
			
			$folio = explode("-",$facfolio);
			if (isset($folio[2])){
			$f1 = $folio[0];
			$f2 = $folio[1];
			$f3 = $folio[2];
			}
			
			$nif = $row['ClientNif'];
			$nomcli = $row['ClientNom'].' '.$row['ClientApePat'].' '.$row['ClientApeMat'];
			$fneto = ($row['FactNet']);
			$fiva = ($row['FactIva']);
			$ftot = $fneto + $fiva;
			
			$totneto += $fneto;
			$totiva += $fiva;
			$tottot += $ftot;
			
			$fec_fac = $row['FactFecFac'];
			$fec_ven = $row['FactFecVe'];
			
			if ($row['CondVenID']=='C000' || trim($fec_ven)==''){
			$fec_ven = $fec_fac;
			}
			
			if ($anulado==1){
			$tipcli = '00';
			$nif = '99999999999';
			$nomcli = 'ANULADO';
			$fneto = '0.00';
			$fiva = '0.00';
			$ftot = '0.00';
			}
			
			$sheet->setCellValue('A'.$start,$fila);
			$sheet->getStyle('A'.$start)->getNumberFormat()->setFormatCode('0000');
			$sheet->setCellValue('B'.$start,getFecPE($fec_fac)); // Fecha Factura
			$sheet->setCellValue('C'.$start,getFecPE($fec_ven)); // Fecha Vencimiento
			$sheet->setCellValue('D'.$start,$pref.$tip_doc );  // Tipo Doc
			$sheet->getStyle('D'.$start)->getNumberFormat()->setFormatCode('00');
			$sheet->setCellValue('E'.$start,$pref.$f2);
			$sheet->getStyle('E'.$start)->getNumberFormat()->setFormatCode('0000');
			$sheet->setCellValue('F'.$start,$pref.$f3);
			$sheet->getStyle('F'.$start)->getNumberFormat()->setFormatCode('000000');
			$sheet->setCellValue('G'.$start,$pref.($tipcli));
			$sheet->getStyle('G'.$start)->getNumberFormat()->setFormatCode('00');
			$sheet->setCellValue('H'.$start,$nif);
			$sheet->setCellValue('I'.$start,$nomcli);
			$sheet->setCellValue('K'.$start,$fneto); // Monto sin IGV
			$sheet->setCellValue('O'.$start,$fiva);
			$sheet->setCellValue('Q'.$start,$ftot);
			// Si tiene algun documento previo referenciado
			if ($row['FacturaID2']>0){
			$factfolio = $row['FactFolio2'];
			//echo $factfolio;
			$folio = split("\-",$factfolio);
				$f1 = '';
				$f2 = '';
				$f3 = '';
				if (isset($folio[2])){
				$f1 = $folio[0];
				$f2 = $folio[1];
				$f3 = $folio[2];
				}
			
			$sheet->setCellValue('S'.$start,getFecPE($row['FactFecFac2']));
			$sheet->setCellValue('T'.$start,$f1);
			$sheet->getStyle('T'.$start)->getNumberFormat()->setFormatCode('00');
			$sheet->setCellValue('U'.$start,$f2);
			$sheet->getStyle('U'.$start)->getNumberFormat()->setFormatCode('0000');
			$sheet->setCellValue('V'.$start,$f3);								
			$sheet->getStyle('V'.$start)->getNumberFormat()->setFormatCode('000000');				
			}
			
			
		}
		$start+= 1;
		$sheet->setCellValue('K'.$start,$totneto); // Monto sin IGV
		$sheet->setCellValue('O'.$start,$totiva);
		$sheet->setCellValue('Q'.$start,$tottot);
		$sheet->setCellValue('A3',$periodo);
		
		
	// Cargar Data
	$res->close();
	$mysqli->check_result();
//	exit;
	// Output File
	#echo date('H:i:s') . " Write to Excel2007 format\n";
	$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
	ob_end_clean();
	header('Content-type: application/vnd.ms-excel');
	header('Content-Disposition: attachment; filename="RegistroVentas.xlsx"');
	$objWriter->save('php://output');
	
	}
}

?>